IF  EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[COIBatch]') AND type in (N'U'))
DROP TABLE [dbo].[COIBatch]
GO
/****** Object:  Table [dbo].[COIBatch]    Script Date: 03/25/2009 15:17:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[COIBatch](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[EmailSentDate] [datetime] NULL,
	[ExceptionText] [varchar](4000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 CONSTRAINT [PK_COIBatch] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

IF  EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[COIBatchPerson]') AND type in (N'U'))
DROP TABLE [dbo].[COIBatchPerson]
GO
/****** Object:  Table [dbo].[COIBatchPerson]    Script Date: 03/25/2009 15:17:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[COIBatchPerson](
	[EmailAddress] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
	[FullName] [varchar](256) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
	[AdmPersonId] [int] NOT NULL,
	[AddCOI] [bit] NOT NULL,
	[COIAddedDate] [datetime] NULL,
	[COIBatchId] [int] NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

IF  EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[AddCOIBatch]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[AddCOIBatch]
GO
/****** Object:  StoredProcedure [dbo].[AddCOIBatch]    Script Date: 03/25/2009 15:17:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[AddCOIBatch]
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON

	INSERT INTO dbo.COIBatch (EmailSentDate, ExceptionText) values(null, null)

	SELECT SCOPE_IDENTITY()
END
GO

IF  EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[GetCOIBatchPeople]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetCOIBatchPeople]
GO
/****** Object:  StoredProcedure [dbo].[GetCOIBatchPeople]    Script Date: 03/25/2009 15:18:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GetCOIBatchPeople]
(
@reminders bit = 0
)
AS
BEGIN
	SET NOCOUNT ON

	SELECT * FROM COIBatchPerson cbp
	LEFT JOIN COIBatch cb on cb.Id = cbp.CoiBatchId
	WHERE (COIAddedDate IS NOT NULL OR @reminders = 1)
	AND emailaddress like '%@%.%'
	AND cb.EmailSentDate IS NULL
END
GO

IF  EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[UpdateCOIBatch]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[UpdateCOIBatch]
GO
/****** Object:  StoredProcedure [dbo].[UpdateCOIBatch]    Script Date: 03/25/2009 15:18:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[UpdateCOIBatch]
(
@coiBatchId int,
@emailSentDate datetime,
@exceptionText varchar(4000) = null
)

AS
BEGIN
	SET NOCOUNT ON;

	UPDATE COIBatch SET EmailSentDate = @emailSentDate, ExceptionText = @exceptionText
	WHERE Id = @coiBatchId
END

GO

IF  EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[UpdateCOIBatchPerson]') 
AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[UpdateCOIBatchPerson]
GO
/****** Object:  StoredProcedure [dbo].[UpdateCOIBatchPerson]    Script Date: 03/25/2009 15:18:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[UpdateCOIBatchPerson]
(@admPersonId int,
@coiAddedDate datetime,
@coiBatchId int = null)
AS
BEGIN
	SET NOCOUNT ON
	Update dbo.COIBatchPerson SET COIAddedDate = @coiAddedDate,
	COIBatchId = @coiBatchId
	WHERE AdmPersonId = @admPersonId
END


